﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;

namespace XamlDom.Runtime
{
    public class AssemblyInfo
    {
        public AssemblyInfo(string assemblyName,string version,string assemblyFullName,string assemblyLocation)
        {
            this.AssemblyName = assemblyName;
            this.Version = version;
            this.AssemblyFullName = assemblyFullName;
            this.AssemblyLocation = assemblyLocation;
            this.XamlNameSpaces = new List<XamlNamespace>();
        }

        public string AssemblyName { get; private set; }
        public string Version { get; private set; }
        public string AssemblyFullName { get; private set; }
        public string AssemblyLocation { get; private set; }

        public Assembly RuntimeAssembly
        {
            get
            {
                return Assembly.LoadFile(this.AssemblyLocation);
            }
        }

        public List<XamlNamespace> XamlNameSpaces { get; private set; }

    }

    public class XamlNamespace
    {
        public XamlNamespace(string xmlNamespace)
        {
            this.XmlNamespace = xmlNamespace;
            this.ClrNamespaces = new List<string>();
        }

        public List<string> ClrNamespaces { get; private set; }
        public string XmlNamespace { get; private set; }
    }

    public class AssemblyLoader
    {
        public static List<AssemblyInfo> assemblyInfos = new List<AssemblyInfo>();

        public static void RegisterAssembly(Assembly assembly)
        {
            AssemblyInfo assemblyInfo = new AssemblyInfo(assembly.GetName().Name, assembly.GetName().Version.ToString(), assembly.FullName, assembly.Location);

            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            foreach (XmlnsDefinitionAttribute xmlnsDef in assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute),true))
            {
                string xmlNamespace = xmlnsDef.XmlNamespace;

                XamlNamespace xamlNameSpace = null;

                List<XamlNamespace> existXamlNameSpaces = assemblyInfo.XamlNameSpaces.Where(x => x.XmlNamespace.Equals(xmlNamespace)).ToList();

                if (existXamlNameSpaces.Count > 0)
                {
                    xamlNameSpace = existXamlNameSpaces.FirstOrDefault();
                    xamlNameSpace.ClrNamespaces.Add(xmlnsDef.ClrNamespace);
                }
                else
                {
                    xamlNameSpace = new XamlNamespace(xmlnsDef.XmlNamespace);
                    xamlNameSpace.ClrNamespaces.Add(xmlnsDef.ClrNamespace);
                    assemblyInfo.XamlNameSpaces.Add(xamlNameSpace);
                }
            }

            assemblyInfos.Add(assemblyInfo);
        }
    }
}
